package com.heyqing.topic.IsAnagram_0242;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * ClassName:IsAnagram
 * Package:com.heyqing.topic.IsAnagram_0242
 * Description:
 *
 * @Date:2024/9/24
 * @Author:Heyqing
 */
public class IsAnagram {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;
        Map<Character, Integer> sMap = new HashMap<>();
        Map<Character, Integer> tMap = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char sc = s.charAt(i);
            char tc = t.charAt(i);
            Integer si = sMap.get(sc);
            Integer ti = tMap.get(tc);
            if (Objects.isNull(si)) sMap.put(sc, 1);
            else sMap.put(sc, ++si);
            if (Objects.isNull(ti)) tMap.put(tc, 1);
            else tMap.put(tc, ++ti);
        }
        Set<Character> set = sMap.keySet();
        for (Character key : set) {
            if (Objects.isNull(tMap.get(key))) return false;
            System.out.println("key:" + key + " sVal:" + sMap.get(key) + " tVal:" + tMap.get(key));
            if (sMap.get(key) - tMap.get(key) != 0) return false;
        }
        return true;
    }
}
